package com.itHsuSE.demo8;

/**
 * @author Hsu琛君珩
 * @ClassName:SimpleCircleCalculator
 * @date 2023-12-20
 * @apiNote
 * @Version: v1.0
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimpleCircleCalculator {
    public static void main(String[] args) {
        // 创建基本组件
        JFrame frame = new JFrame("Circle Calculator");
        JTextField radiusInput = new JTextField(10);
        JButton calculateButton = new JButton("计算");
        JLabel resultLabel = new JLabel("结果显示在这里");

        // 设置布局并添加组件
        frame.setLayout(new FlowLayout());
        frame.add(radiusInput);
        frame.add(calculateButton);
        frame.add(resultLabel);
        frame.setSize(250, 120);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        // 添加按钮的事件监听器
        calculateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    double radius = Double.parseDouble(radiusInput.getText());
                    double area = Math.PI * radius * radius;
                    double perimeter = 2 * Math.PI * radius;
                    resultLabel.setText("面积: " + area + ", 周长: " + perimeter);
                } catch (NumberFormatException ex) {
                    resultLabel.setText("请输入有效的半径");
                }
            }
        });
    }
}
